import os
import threading
from PIL import Image

# 源目录和输出目录
input_dir = "images/input"
output_dir = "images/output"
os.makedirs(output_dir, exist_ok=True)

# 处理函数
def resize_image(filename, size=(200, 200)):
    try:
        img_path = os.path.join(input_dir, filename)
        img = Image.open(img_path)
        img = img.resize(size)
        output_path = os.path.join(output_dir, filename)
        img.save(output_path)
        print(f"{filename} resized.")
    except Exception as e:
        print(f"Error processing {filename}: {e}")

# 多线程处理函数
def process_images_multithreaded():
    threads = []
    for filename in os.listdir(input_dir):
        if filename.lower().endswith((".png", ".jpg", ".jpeg")):
            t = threading.Thread(target=resize_image, args=(filename,))
            threads.append(t)
            t.start()

    # 等待所有线程结束
    for t in threads:
        t.join()

if __name__ == "__main__":
    process_images_multithreaded()
